from PIL import Image, ImageDraw, ImageFont 
import pandas as pd 
#  生成表格的
# 生成表格数据（示例为2025年Q3销售数据）
df = pd.DataFrame({
    "地区": ["华东"],
    "销售额(万)": [1210],
    "增长率": ["1.5%"]
})
# df = pd.DataFrame({
#     "地区": ["华东", "华南"],
#     "销售额(万)": [1210, 920],
#     "增长率": ["1.5%", "5.7%"]
# })
# df = pd.DataFrame({
#     "地区": ["华东", "华南", "华北"],
#     "销售额(万)": [1210, 920, 840],
#     "增长率": ["1.5%", "5.7%", "1.2%"]
# }) 
# 转换为图片（支持中文）
font = ImageFont.truetype("C:\Windows\Fonts\msyh.ttc",  24)  # 2025推荐字体 
img = Image.new("RGB",  (600, 100), (255, 255, 255))
draw = ImageDraw.Draw(img)
draw.text((10,  10), "2026年三季度销售报表", font=font, fill=(0, 0, 0))
 
# 绘制表格（含边框和文字）
for i, row in df.iterrows(): 
    y_pos = 48 + i * 40 
    draw.rectangle([10,  y_pos, 590, y_pos + 30], outline=(0, 0, 0))
    draw.text((20,  y_pos ), str(row[0]), font=font, fill=(0, 0, 0))
    draw.text((200,  y_pos ), str(row[1]), font=font, fill=(0, 0, 128))
    draw.text((400,  y_pos ), str(row[2]), font=font, fill=(128, 0, 0))
img.save("table1.png") 